package mi.top.utils;

import cn.hutool.core.date.DateUtil;
import org.eclipse.paho.client.mqttv3.MqttException;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

/**
 * 光栅
 *
 * @author 祥霸
 * @date 2021.07.13
 *
 * */
public class RasterSwing {

    private JTextArea textArea;

    private TextField productKey;
    private TextField deviceName;
    private TextField deviceSecret;

    /** 遮挡*/
    private JButton occlude;

    /** 未遮挡*/
    private JButton unOcclude;

    private JButton init;

    MqttUtils mqttUtils;

    public void init() {
        // 确保一个漂亮的外观风格

        JFrame frame = new JFrame();
        frame.setTitle("光栅");

        JPanel jp = new JPanel();
        jp.setSize(100, 200);

        JLabel label = new JLabel("productKey");
        productKey = new TextField(16);
        productKey.setText("");
        jp.add(label);
        jp.add(productKey);


        label = new JLabel("deviceName");
        deviceName = new TextField(16);
        deviceName.setText("");

        jp.add(label);
        jp.add(deviceName);

        label = new JLabel("deviceSecret");
        deviceSecret = new TextField(16);
        deviceSecret.setText("");
        jp.add(label);
        jp.add(deviceSecret);

        init = new JButton("init");

        init.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                System.out.println("init:click");
                System.out.println("productKey：" + productKey.getText());
                System.out.println("deviceName：" + deviceName.getText());
                System.out.println("deviceSecret：" + deviceSecret.getText());

                synchronized (RasterSwing.class) {
                    if(mqttUtils != null){
                        return;
                    }

                    try {
                        System.out.println("MqttUtils:init");

                        mqttUtils = new MqttUtils(productKey.getText(), deviceName.getText(), deviceSecret.getText());
                        mqttUtils.init(mqttUtils.sign());

                        System.out.println("setBackground");
                        init.setBackground(new Color(0, 0, 0));

                        JOptionPane.showMessageDialog(null, "连接阿里云成功！");
                    } catch (MqttException ex) {
                        ex.printStackTrace();
                        System.out.println("连接失败");
                        JOptionPane.showMessageDialog(null, "连接阿里云失败！");
                    }
                }
                System.out.println("MqttUtils:init:end");
            }
        });

        jp.add(init);

        occlude = new JButton("未遮挡 -> 遮挡");
        occlude.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                System.out.println("未遮挡 -> 遮挡:click");
                try {
                    mqttUtils.publish("114400200001020000683f");
                    appText(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss") + ": 未遮挡 -> 遮挡");

                } catch (MqttException ex) {
                    ex.printStackTrace();
                }
            }

        });
        jp.add(occlude);

        unOcclude = new JButton("遮挡 -> 未遮挡");
        unOcclude.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                System.out.println("遮挡 -> 未遮挡:click");

                try {
                    mqttUtils.publish("11440020000102ff0029cf");

                    appText(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss") + ": 遮挡 -> 未遮挡");
                } catch (MqttException ex) {
                    ex.printStackTrace();
                }
            }

        });
        jp.add(unOcclude);

        textArea = new JTextArea("",5,20);
        textArea.setLineWrap(true);
        textArea.setSize(300,300);
        JScrollPane scroll = new JScrollPane(textArea);
        //把定义的JTextArea放到JScrollPane里面去

        //分别设置水平和垂直滚动条自动出现
        scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        scroll.setSize(300, 300);
        jp.add(scroll);

        JButton clean = new JButton("clean");

        clean.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                textArea.setText("");
            }
        });
        jp.add(clean);

        jp.setVisible(true);


        frame.add(jp);
        frame.setSize(300,300);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void appText(String txt) {
        textArea.append(txt);
        textArea.append("\n");
    }

    public JPanel getJPanel() {
        // 确保一个漂亮的外观风格

        JPanel jp = new JPanel();
        JLabel label = new JLabel("productKey");
        productKey = new TextField(16);
        productKey.setText("");
        jp.add(label);
        jp.add(productKey);


        label = new JLabel("deviceName");
        deviceName = new TextField(16);
        deviceName.setText("");

        jp.add(label);
        jp.add(deviceName);

        label = new JLabel("deviceSecret");
        deviceSecret = new TextField(16);
        deviceSecret.setText("");
        jp.add(label);
        jp.add(deviceSecret);

        init = new JButton("init");

        init.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                System.out.println("init:click");
                System.out.println("productKey：" + productKey.getText());
                System.out.println("deviceName：" + deviceName.getText());
                System.out.println("deviceSecret：" + deviceSecret.getText());

                synchronized (RasterSwing.class) {
                    if(mqttUtils != null){
                        return;
                    }

                    try {
                        System.out.println("MqttUtils:init");

                        mqttUtils = new MqttUtils(productKey.getText(), deviceName.getText(), deviceSecret.getText());
                        mqttUtils.init(mqttUtils.sign());

                        init.setBackground(new Color(0, 0, 0));

                        JOptionPane.showMessageDialog(null, "连接阿里云成功！");
                    } catch (MqttException ex) {
                        ex.printStackTrace();
                        System.out.println("连接失败");
                        JOptionPane.showMessageDialog(null, "连接阿里云失败！");
                    }
                }
                System.out.println("MqttUtils:init:end");
            }
        });

        jp.add(init);

        JButton button = new JButton("未遮挡 -> 遮挡");
        button.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                System.out.println("未遮挡 -> 遮挡:click");
                try {
                    mqttUtils.publish("114400200001020000683f");
                    appText(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss") + ": 未遮挡 -> 遮挡");

                } catch (MqttException ex) {
                    ex.printStackTrace();
                }
            }

        });
        jp.add(button);

        button = new JButton("遮挡 -> 未遮挡");
        button.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                System.out.println("遮挡 -> 未遮挡:click");

                try {
                    mqttUtils.publish("11440020000102ff0029cf");

                    appText(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss") + ": 遮挡 -> 未遮挡");
                } catch (MqttException ex) {
                    ex.printStackTrace();
                }
            }

        });
        jp.add(button);

        textArea = new JTextArea("",5,20);
        textArea.setLineWrap(true);
        textArea.setSize(300,300);
        JScrollPane scroll = new JScrollPane(textArea);
        //把定义的JTextArea放到JScrollPane里面去

        //分别设置水平和垂直滚动条自动出现
        scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        scroll.setSize(300, 300);
        jp.add(scroll);

        JButton clean = new JButton("clean");

        clean.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                textArea.setText("");
            }
        });
        jp.add(clean);
        return jp;
    }
}
